package com.common.aop.utils;

import com.common.aop.UserAgentTypeEnum;

import java.util.regex.Pattern;

/**
 * Created by sola on 2018/1/21.
 *
 * User-Agent 工具
 */

public class UserAgentTools {

    private static Pattern PHONE_PATTERN = Pattern.compile("(Windows Phone|Android|iPhone|iPod)");
    private static Pattern TABLET_PATTERN = Pattern.compile("(iPad)");
    /**
     * 识别设备类型
     * @param userAgent 设备标识
     * @return 设备类型
     */
    public static Integer recognize(String userAgent){
        if(PHONE_PATTERN.matcher(userAgent).find()){
            return UserAgentTypeEnum.PHONE.getCode();
        }
        if(TABLET_PATTERN.matcher(userAgent).find()){
            return UserAgentTypeEnum.TABLET.getCode();
        }
        return UserAgentTypeEnum.PC.getCode();
    }

}
